package Leetcode.lc059;

/***
 * 螺旋矩阵
 */
public class Sloution {
    public int[][] generateMatrix(int n) {
         int limit=n/2;
         int startx=0,starty=0,offset=1,loop=0;
         int num=1;
        int[][] res = new int[n][n];
        while(loop<limit){
             int i=startx;
             int j=starty;
             for(;j<n-offset;j++){
                 res[i][j]=num++;
             }
            for(;i<n-offset;i++){
                res[i][j]=num++;
            }
            for(;j>starty;j--){
                res[i][j]=num++;
            }
            for(;i>startx;i--){
                res[i][j]=num++;
            }
            starty++;
            startx++;
            loop++;
            offset++;
         }
        if(n%2==1){
            res[startx][starty]=num;
        }
        return res;
    }

    public static void main(String[] args) {
        Sloution sloution = new Sloution();
        int[][] ints = sloution.generateMatrix(3);
        for(int i=0;i<ints.length;i++){
            System.out.println(ints[i]);
        }
    }
}
